import 'package:flutter/material.dart';
import 'package:charts_flutter/flutter.dart' as charts;
import 'package:flutter/rendering.dart';
import "package:flare_flutter/flare_actor.dart";
import 'package:flutter_screenutil/flutter_screenutil.dart';
class Test extends StatefulWidget{
    _TestState createState() => _TestState();
}

class _TestState extends State<Test> {

    String _year;
    int _sales;

     _onSelectionChanged(charts.SelectionModel model) {
        final selectedDatum = model.selectedDatum;

        for(var item in selectedDatum){
            print(item);
        }
        setState(() {
            _year = selectedDatum.first.datum.year;
            _sales = selectedDatum.first.datum.sales;
        });
    }

    

  @override
  Widget build(BuildContext context) {

    return new Scaffold(
        appBar: AppBar(
            title: Text("图表"),
            centerTitle: true,
        ),
        body:Container(
            child: Column(
                children: <Widget>[
                    Row(
                        children: <Widget>[
                            Expanded(
                                child: Container(
                                    alignment: Alignment.center,
                                    child: Text("年份：${_year}"),
                                ),
                            ),
                            Expanded(
                                child: Container(
                                    alignment: Alignment.center,
                                    child: Text("数值：${_sales}"),
                                ),
                            )
                        ],
                    ),
                    Container(
                        width: double.infinity,
                        height: 200.0,
                        child: charts.BarChart(
                            ChartFlutterBean.createSampleData(),
                            selectionModels: [
                                charts.SelectionModelConfig(
                                    type: charts.SelectionModelType.info,
                                    changedListener: _onSelectionChanged,
                                )
                            ],
                        ),
                    ),
                    Container(
                        child: Icon(IconData(0xe660, fontFamily: "Iconfont"), size: 24.0)
                    ),
                    Container(
                        width: double.infinity,
                        height: ScreenUtil().setHeight(300),
                        child: FlareActor(
                            "assets/Filip.flr",
                            alignment: Alignment.center,
                            fit: BoxFit.contain,
                            animation: 'loading',
                        ),
                    )
                ],
            ),
        )     
    );
  }


}

class OrdinalSales {
  final String year;
  final int sales;

  OrdinalSales(this.year, this.sales);
}


class ChartFlutterBean {

  static List<charts.Series<OrdinalSales, String>> createSampleData() {
    final data = [
      new OrdinalSales('2014', 5),
      new OrdinalSales('2015', 25),
      new OrdinalSales('2016', 100),
      new OrdinalSales('2017', 75),
    ];

    return [
      new charts.Series<OrdinalSales, String>(
        id: 'Sales',
        colorFn: (_, __) => charts.MaterialPalette.blue.shadeDefault,
        domainFn: (OrdinalSales sales, _) => sales.year,
        measureFn: (OrdinalSales sales, _) => sales.sales,
        data: data,
      )
    ];
  }
}